package study.javaConstructorTest.classPackage;

import java.lang.reflect.Constructor;

/**
 * @author: chengchao
 * @description:
 * @date: create in 2021/9/10
 **/
public class B {
    public static void main(String[] args) {
        B b = new B();
        b.newInstanceByContructorNewInstance();
    }

    private void newInstanceByContructorNewInstance() {
        try {
            Class c = Class.forName("study.javaConstructorTest.classPackage.A");
            // 调用无参的、私有构造函数
            Constructor c0 = c.getDeclaredConstructor();
            c0.setAccessible(true);
            A a0 = (A) c0.newInstance();
            a0.printInfo();

            // 调用带参的、私有构造函数
            Constructor c1 = c.getDeclaredConstructor(new Class[]{int.class, int.class});
            c1.setAccessible(true);
            A a1 = (A) c1.newInstance(new Object[]{5, 6});
            a1.printInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
